/*
 * Galaxium Messenger
 * 
 * Copyright (C) 2007 Philippe Durand <draekz@gmail.com>
 * 
 * License: GNU General Public License (GPL)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

using System;
using System.Collections.Generic;

using Gtk;

using Galaxium.Core;
using Galaxium.Gui;
using Galaxium.Gui.GtkGui;
using Galaxium.Client;

namespace Galaxium.Protocol.GaduGadu.GtkGui
{
	public class SetNicknameCommand : AbstractMenuCommand
	{
		public override void SetMenuItem ()
		{
			
		}
		
		public override void Run ()
		{
			//TreeNodeContext context = Object as TreeNodeContext;
			//GaduContact contact = context.NodeObject as GaduContact;
			
			//SetNicknameDialog dialog = new SetNicknameDialog(SessionUtility.ActiveSession, contact);
		}
	}
	
	public class DenyAuthCommand : AbstractMenuCommand
	{
		public override void SetMenuItem ()
		{
			//TreeNodeContext context = Object as TreeNodeContext;
			//GaduContact contact = context.NodeObject as GaduContact;
		}
		
		public override void Run ()
		{
			if (SessionUtility.ActiveSession is GaduSession)
			{
				//TreeNodeContext context = Object as TreeNodeContext;
				//GaduContact contact = context.NodeObject as GaduContact;
				
				//GaduSession session = SessionUtility.ActiveSession as GaduSession;
				
				//session.EvaluateSubscription (contact.UniqueIdentifier, false);
			}
		}
	}
	
	public class RequestAuthCommand : AbstractMenuCommand
	{
		public override void SetMenuItem ()
		{
			//TreeNodeContext context = Object as TreeNodeContext;
			//GaduContact contact = context.NodeObject as GaduContact;
			
			/*if (contact.Subscription == SubscriptionState.Both)
				(MenuItem as MenuItem).Sensitive = false;
			else if (contact.Subscription == SubscriptionState.To)
				(MenuItem as MenuItem).Sensitive = false;*/
		}
		
		public override void Run ()
		{
			if (SessionUtility.ActiveSession is GaduSession)
			{
				//TreeNodeContext context = Object as TreeNodeContext;
				//GaduContact contact = context.NodeObject as GaduContact;
				
				//GaduSession session = SessionUtility.ActiveSession as GaduSession;
				
				//session.RequestSubscribe (contact);
			}
		}
	}
	
	public class RemoveAuthCommand : AbstractMenuCommand
	{
		public override void SetMenuItem ()
		{
			//TreeNodeContext context = Object as TreeNodeContext;
			//GaduContact contact = context.NodeObject as GaduContact;
			
			//if (contact.Subscription == SubscriptionState.From)
				//(MenuItem as MenuItem).Sensitive = false;
		}
		
		public override void Run ()
		{
			if (SessionUtility.ActiveSession is GaduSession)
			{
				//TreeNodeContext context = Object as TreeNodeContext;
				//GaduContact contact = context.NodeObject as GaduContact;
				
				//GaduSession session = SessionUtility.ActiveSession as GaduSession;
				
				//session.RequestUnsubscribe (contact);
			}
		}
	}
	
	public class RemoveFromGroupCommand : AbstractMenuCommand
	{
		public override void SetMenuItem ()
		{
			
		}
		
		public override void Run ()
		{
			//TreeNodeContext context = Object as TreeNodeContext;
			//GaduContact contact = context.NodeObject as GaduContact;
			//GaduGroup group = context.TreeBuilder.GetParentObject (context.Node) as GaduGroup;
			
			/*if (SessionUtility.ActiveSession is GaduSession)
			{
				RemoveContactDialog dialog = new RemoveContactDialog((GaduSession)SessionUtility.ActiveSession, group.UniqueIdentifier == "0");
				ResponseType response = ResponseType.None;
				
				while(response == ResponseType.None)
				{
					response = (ResponseType)dialog.Run();
					
					if (response == ResponseType.Apply)
					{
						string error = String.Empty;
						bool success = false;
						
						// Block the user ahead of time, before we attempt to remove him.
						GaduSession session = SessionUtility.ActiveSession as GaduSession;
						
						if (dialog.RemoveAll)
							session.RemoveContact (contact.UniqueIdentifier);
						else
							session.RemoveContactFromGroup (contact.UniqueIdentifier, group.UniqueIdentifier);
						
						dialog.Destroy();
						break;
					}
					else
					{
						dialog.Destroy();
						break;
					}
				}
			}*/
		}
	}
	
	public class MoveToGroupCommand : AbstractMenuCommand
	{
		public override void SetMenuItem ()
		{
			
		}
		
		public override void Run ()
		{
			if (SessionUtility.ActiveSession is GaduSession)
			{
				//TreeNodeContext context = Object as TreeNodeContext;
				//GaduContact contact = context.NodeObject as GaduContact;
				//GaduGroup group = context.TreeBuilder.GetParentObject (context.Node) as GaduGroup;
				
				/*CopyMoveGroupDialog dialog = new CopyMoveGroupDialog((GaduSession)SessionUtility.ActiveSession, contact, false);
				ResponseType response = ResponseType.None;
				
				while(response == ResponseType.None)
				{
					response = (ResponseType)dialog.Run();
					
					if (response == ResponseType.Apply)
					{
						string error = String.Empty;
						bool success = false;
						
						GaduSession session = SessionUtility.ActiveSession as GaduSession;
						success = session.MoveContactToGroup(contact.UniqueIdentifier, group.UniqueIdentifier, dialog.SelectedGroupID);

						if (!success)
						{
							MessageDialog errordialog = new MessageDialog (null, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, error);
							errordialog.Run ();
							errordialog.Destroy ();
							response = ResponseType.None;
							continue;
						}
						
						dialog.Destroy();
						break;
					}
					else
					{
						dialog.Destroy();
						break;
					}
				}*/
			}
		}
	}
	
	public class CopyToGroupCommand : AbstractMenuCommand
	{
		public override void SetMenuItem ()
		{
			
		}
		
		public override void Run ()
		{
			if (SessionUtility.ActiveSession is GaduSession)
			{
				//TreeNodeContext context = Object as TreeNodeContext;
				//GaduContact contact = context.NodeObject as GaduContact;
				//GaduGroup group = context.TreeBuilder.GetParentObject (context.Node) as GaduGroup;
				
				/*CopyMoveGroupDialog dialog = new CopyMoveGroupDialog((GaduSession)SessionUtility.ActiveSession, contact, true);
				ResponseType response = ResponseType.None;
				
				while(response == ResponseType.None)
				{
					response = (ResponseType)dialog.Run();
					
					if (response == ResponseType.Apply)
					{
						string error = String.Empty;
						bool success = false;
						
						GaduSession session = SessionUtility.ActiveSession as GaduSession;
						if (group.UniqueIdentifier == "0")
							success = session.MoveContactToGroup (contact.UniqueIdentifier, group.UniqueIdentifier, dialog.SelectedGroupID);
						else
							success = session.AddContactToGroups(contact.UniqueIdentifier, dialog.SelectedGroupID);
						
						if (!success)
						{
							MessageDialog errordialog = new MessageDialog (null, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, error);
							errordialog.Run ();
							errordialog.Destroy ();
							response = ResponseType.None;
							continue;
						}
						
						dialog.Destroy();
						break;
					}
					else
					{
						dialog.Destroy();
						break;
					}
				}*/
			}
		}
	}
	
	public class SupressNameCommand : AbstractMenuCommand
	{
		public override void SetMenuItem ()
		{
			TreeNodeContext context = Object as TreeNodeContext;
			GaduContact contact = context.NodeObject as GaduContact;
			
			if (contact != null)
				(MenuItem as CheckMenuItem).Active = contact.SupressName;
		}
		
		public override void Run ()
		{
			TreeNodeContext context = Object as TreeNodeContext;
			GaduContact contact = context.NodeObject as GaduContact;
			
			if (contact != null)
			{
				contact.SupressName = (MenuItem as CheckMenuItem).Active;
				contact.Save();
			}
		}
	}
	
	public class SupressImageCommand : AbstractMenuCommand
	{
		public override void SetMenuItem ()
		{
			TreeNodeContext context = Object as TreeNodeContext;
			GaduContact contact = context.NodeObject as GaduContact;
			
			if (contact != null)
				(MenuItem as CheckMenuItem).Active = contact.SupressImage;
		}
		
		public override void Run ()
		{
			TreeNodeContext context = Object as TreeNodeContext;
			GaduContact contact = context.NodeObject as GaduContact;
			
			if (contact != null)
			{
				contact.SupressImage = (MenuItem as CheckMenuItem).Active;
				contact.Save();
			}
		}
	}
	
	public class AutoAcceptCommand : AbstractMenuCommand
	{
		public override void SetMenuItem ()
		{
			TreeNodeContext context = Object as TreeNodeContext;
			GaduContact contact = context.NodeObject as GaduContact;
			
			if (contact != null)
				(MenuItem as CheckMenuItem).Active = contact.AutoAccept;
		}
		
		public override void Run ()
		{
			TreeNodeContext context = Object as TreeNodeContext;
			GaduContact contact = context.NodeObject as GaduContact;
			
			if (contact != null)
			{
				contact.AutoAccept = (MenuItem as CheckMenuItem).Active;
				contact.Save();
			}
		}
	}
	
	public class SendFileCommand : AbstractMenuCommand
	{
		public override void SetMenuItem ()
		{
			TreeNodeContext context = Object as TreeNodeContext;
			
			if(context != null)
			{
				GaduContact contact = context.NodeObject as GaduContact;
			
			if (contact != null)
			{
				if (contact.Presence == GaduPresence.Offline)
					(MenuItem as MenuItem).Sensitive = false;
			}
			}
		}
		
		public override void Run ()
		{
			TreeNodeContext context = Object as TreeNodeContext;
			GaduContact contact = context.NodeObject as GaduContact;
			
			if (contact != null)
			{
				// Spark a select file dialog.
				FileChooserDialog dialog = new FileChooserDialog("Select File", null, FileChooserAction.Open, "Cancel", ResponseType.Cancel, "Select", ResponseType.Accept);
				dialog.SetCurrentFolder(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
				
				if (dialog.Run () == (int)ResponseType.Accept)
					if (dialog.Filename != null && dialog.Filename.Length > 0)
					{
						contact.Session.SendFile (contact, dialog.Filename);
					}
				
				dialog.Destroy ();
			}
		}
	}
	
	public class ViewProfileCommand : AbstractMenuCommand
	{
		public override void SetMenuItem ()
		{
			
		}
		
		public override void Run ()
		{
			
		}
	}
}